limsTrainEffect.editPage = {

    getGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.trainEffectGrid,
            url: _this.urlManager.trainEffectUrl + '/queries',
            service: 'limsTrainEffectServiceImpl',
            dbTable: 'T_LIMS_TRAIN_EFFECT',
            columns: limsTrainEffect.getEditListColumns(true),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshAttachments(_this.idManager.trainEffectGrid)
                }
            },
            deleteFormatter: function (row) {
                return row.ext$.traintopics || row.id
            },
            onRowActive: function (index, row) {
                _this.refreshAttachments(_this.idManager.trainEffectGrid)
            },
            requestData: {
                status_DIFFER: 'finish'
            },
            page: true,
            toolbar: [{
                type: 'button',
                text: 'GIKAM.BUTTON.INSERT',
                icon: 'add',
                onClick: function () {
                    Gikam.preInsert({
                        modalTitle: 'BASEMODULE.RM.TRAINS.MODAL.ADD',
                        fields: limsTrainEffect.getBaseInfoFormFields(),
                        url: _this.urlManager.trainEffectUrl,
                        titleWidth: 90
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.trainEffectGrid).refresh()
                    })
                }
            }, {
                type: 'button',
                text: 'GIKAM.BUTTON.DELETE',
                icon: 'remove-row',
                onClick: function () {
                    Gikam.getComp(_this.idManager.trainEffectGrid).deleteRows(_this.urlManager.trainEffectUrl)
                }
            }, {
                type: 'button',
                text: 'BASEMODULE.RM.TRAINS.BUTTON.COMPLETE',
                icon: 'pass',
                onClick: function () {
                    Gikam.getComp(_this.idManager.trainEffectGrid).processRows({
                        url: _this.urlManager.trainEffectUrl + '/action/finish',
                        confirm: {
                            msg: 'BASEMODULE.RM.TRAINS.BUTTON.COMPLETE', // 确认弹出框中左侧的信息
                            title: 'BASEMODULE.RM.TRAINS.BUTTON.COMPLETE', // 确认弹出框中的标题
                        },
                        formatter: function (row) {
                            return row.ext$.traintopics
                        },
                        method: 'put'
                    }).done(function (id) {
                        Gikam.getComp(_this.idManager.trainEffectGrid).refresh()
                    })
                }
            }]
        }
    },

    getDoneGridParam: function () {
        var _this = this
        return {
            type: 'grid',
            id: _this.idManager.trainEffectDoneGrid,
            url: _this.urlManager.trainEffectUrl + '/queries',
            service: 'limsTrainEffectServiceImpl',
            dbTable: 'T_LIMS_TRAIN_EFFECT',
            columns: limsTrainEffect.getEditListColumns(false),
            onLoadSuccess: function (data) {
                if (!Gikam.isEmpty(data)) {
                    this.activeRowByIndex(0)
                } else {
                    _this.refreshAttachments(_this.idManager.trainEffectDoneGrid)
                }
            },
            requestData: {
                status_EQ: 'finish'
            },
            onRowActive: function (index, row) {
                _this.refreshAttachments(_this.idManager.trainEffectDoneGrid)
            },
            page: true,
            toolbar: []
        }
    },

    getAttachUploaderId: function (gridId) {
        return gridId + '-uploader'
    },

    refreshAttachments: function (gridId) {
        let _this = this
        let grid = Gikam.getComp(gridId)
        let actRow = grid.getActivedRow()
        Gikam.getAsyncComp(_this.getAttachUploaderId(gridId)).done(uploader => {
            if (actRow && actRow.id) {
                uploader.setOptions({
                    bizId: actRow.id,
                })
            } else {
                uploader.setOptions({
                    bizId: -1,
                })
            }
        })
    },

    getAttachmantStruct: function (gridId, dbTable, readonly) {
        let _this = this
        return {
            type: 'uploader',
            immediateUpload: true,
            id: _this.getAttachUploaderId(gridId),
            dbTable: dbTable,
            filter: false,
            genericQuery: false,
            readonly: readonly,
            onBeforeUpload: function () {
                var activedRow = Gikam.getComp(gridId).getActivedRow()
                if (Gikam.isEmpty(activedRow)) {
                    Gikam.alert('GIKAM.TIP.CHOOSE_AT_LEAST_ONE_ITEM', 200)
                    return false
                }
            },
        }
    },

    getBaseInfoTab: function () {
        var _this = this
        var first = false
        return {
            type: 'tab',
            panels: [{
                title: 'BASEMODULE.RM.TRAINS.TAB.UN_DONE',
                items: [{
                    type: 'layout',
                    north: {
                        height: '60%',
                        items: [_this.getGridParam()]
                    },
                    center: {
                        items: [_this.getAttachmantStruct(_this.idManager.trainEffectGrid, 'T_LIMS_TRAIN_EFFECT', false)]
                    }
                }]
            }, {
                title: 'BASEMODULE.RM.TRAINS.TAB.DONE',
                items: [{
                    type: 'layout',
                    north: {
                        height: '60%',
                        items: [_this.getDoneGridParam()]
                    },
                    center: {
                        items: [_this.getAttachmantStruct(_this.idManager.trainEffectDoneGrid, 'T_LIMS_TRAIN_EFFECT', true)]
                    }
                }]
            }],
            onActived: function (data) {
                switch (data.index) {
                    case 0:
                        if (first) {  // 首次加载页面时，不操作
                            Gikam.getAsyncComp(_this.idManager.trainEffectGrid).done(function (grid) {
                                grid.refresh()
                            })
                        }
                        break
                    case 1:
                        Gikam.getAsyncComp(_this.idManager.trainEffectDoneGrid).done(function (grid) {
                            grid.refresh()
                        })
                        break
                }
                first = true
            }
        }
    },

    idManager: {
        trainEffectGrid: 'lims-train-effect-edit-list-grid',
        trainEffectDoneGrid: 'lims-train-effect-edit-list-grid' + '-done',
    },

    urlManager: {
        trainEffectUrl: limsTrainEffect.baseUrl,
    },

    create: function () {
        var _this = this
        Gikam.create('layout', {
            id: 'lims-train-effect-edit-list-layout',
            renderTo: workspace.window.$dom,
            center: {
                items: [this.getBaseInfoTab()]
            }
        })
    },

    init: function () {
        this.create()
    }
}
